package arrays;


import java.util.ArrayList;
import java.util.Arrays;

/**
 *
 */
public class MyArrayList {

    //存放数据
    private String[] data;

    //元素个数
    private int size;

    //初始化数组容量
    public MyArrayList(int initialCapacity){
        if (initialCapacity > 0) {
            this.data = new String[initialCapacity];
        } else if (initialCapacity == 0) {
            this.data = new String[0];
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                    initialCapacity);
        }
    }

    public void add(String e){
      int minCapacity = size + 1;
        //判断是否需要扩容
        if(minCapacity > data.length){
            //需要进行扩容
            System.out.println("需要进行扩容");
            int oldCapacity = data.length;
            int newCapacity = oldCapacity + (oldCapacity >> 1);
            if (newCapacity - minCapacity < 0){
                newCapacity = minCapacity;
            }
            data = Arrays.copyOf(data, newCapacity);
        }
        data[size] = e;
        size++;
    }

    public void add(int index,String e){
        //下标越界
        if (index > size || index < 0){
            throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
        }
        int minCapacity = size + 1;
        //判断是否需要扩容
        if(minCapacity > data.length){
            //扩容两倍
            int newSize = minCapacity * 2;
            //需要进行扩容
            String[] temp =  new String[newSize];
            for (int i = 0; i < data.length; i++) {
                //将老数组的值赋值给新数组
                temp[i] = data[i];
            }
            //将扩容后的数组赋值给 data
            data = temp;
        }
        //把 index 后的数据往后移一个
        for(int i = data.length - 1; i > index; i--){
            //将前一个元素赋值给后面一个元素
            data[i] = data[i - 1];
        }
        data[index] = e;
        size++;
    }

    public void remove(int index){
        //下标越界
        if (index > size || index < 0){
            throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
        }
        for(int i = index ; i < data.length ; i++){
            if(i == data.length - 1){
                //将最后一个元素赋值为 null
                data[i] = null;
            }else{
                //将后一个元素赋值给前一个元素
                data[i] = data[i + 1];
            }
        }
        size--;
    }

    public void update(Integer index,String e){
        //下标越界
        if (index > size || index < 0){
            throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
        }
        data[index] = e;
    }

    public String get(Integer index){
        //下标越界
        if (index > size || index < 0){
            throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
        }
        return data[index];
    }

    public void print(){
        System.out.println("size:" + size);
        for(int i = 0 ; i < size ; i++){
            System.out.print(data[i] + " ");
        }
        System.out.println();
    }
}

class Test{
    public static void main(String[] args) {
        MyArrayList myArrayList = new MyArrayList(2);
        myArrayList.add("fefef");
        myArrayList.add("a");
        myArrayList.add("b");
        myArrayList.add("c");
        myArrayList.add(4,"abc");
        myArrayList.print();
        System.out.println("删除======");
        myArrayList.remove(2);
        myArrayList.print();
        System.out.println("修改======");
        myArrayList.update(3,"cxyxj");
        myArrayList.print();
        System.out.println("获取======");
        System.out.println(myArrayList.get(2));
    }
}